﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.XtraRichEdit.Model;
using RR;

public partial class Default2 : System.Web.UI.Page
{
    protected RegistRar regrar = new RegistRar();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt1.Date = DateTime.Now.AddDays(-1);
            dt2.Date = DateTime.Now;

        }
        if (Profile.UserInfo.ActiveRole == 2)
            cbb_kontr.Visible = false;
    }

    private void GetDocList()
    {
        //int kontr = Profile.UserInfo.ActiveKontragent;
        int kontr = -1;
        if (!cbb_kontr.Visible)
        {
            kontr = Profile.UserInfo.ActiveKontragent;
        }
        else
        {
            kontr = Convert.ToInt16(cbb_kontr.Value);
        }
        int docType = Convert.ToInt16(cbb_DocType.Value);
        string clause = string.Empty;
        DateTime d1 = dt1.Date;
        DateTime d2 = dt2.Date;

            var query =
                regrar.DOGDOC.Where(
                    d => d.DOGDOP.DOG1.KONTRAGENT == kontr &&
                        ((docType > 0 && d.DOCTYPE == docType) || (docType == 0 && d.DOCTYPE == d.DOCTYPE)) &&
                        ((d1 != null && d.DATA >= d1 && d.DATA <= d2) || (d1 == null && d.DATA == d.DATA))).Select(d => new
                        {
                            CODE = d.CODE,
                            DOCTYPE = d.DOCTYPE1.NAME,
                            DOPDOG = d.DOPDOG,
                            NOMER = d.NOMER,
                            DATA = d.DATA,
                            PAGENUM = d.PAGENUM,
                            NOTE = d.NOTE,
                            ARCPLACE = d.ARCPLACE,
                            STATE = d.STATE
                        });
            grid.DataSource = query.ToList();
            grid.DataBind();
            return;            
        
        //проверяем номер
        if (txt_DocNum.Value != null)
        {
            clause = String.Format("NOMER =={0} AND {1}", txt_DocNum.Value, kontr);
            ds_dogdoc.DataBind();
            return;
        }
        //тип документа
        if (docType > -1)
        {
            clause = string.Format("DOCTYPE={0}", docType);
        }
        //период
        if (dt1.Value != null & dt2.Value != null)
        {
            if (clause != string.Empty)
            {
                clause += string.Format(" AND DATA BETWEEN {0} AND {1}", dt1.Date, dt2.Date);
            }
            else
            {
                clause = string.Format("DATA BETWEEN {0} AND {1}", dt1.Date, dt2.Date);
            }
        }
        //котрагент
        if (clause == string.Empty) { throw new Exception("Укажите хотя бы один параметр!"); }
        else
        {
            clause += "";
        }

        ds_dogdoc.DataBind();

    }
    protected void cbb_DocType_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void ASPxFormLayout1_E1_Click(object sender, EventArgs e)
    {
        GetData(grid.PageIndex, grid.SettingsPager.PageSize);
    }
    protected void btn2_Click(object sender, EventArgs e)
    {
        GetDataByNum(grid.PageIndex, grid.SettingsPager.PageSize);
    }

    private void GetDataByNum(int iBeginRowIndex, int iMaximumRows)
    {
        int kontr = -1;
        if (!cbb_kontr.Visible)
        {
            kontr = Profile.UserInfo.ActiveKontragent;
        }
        else
        {
            kontr = Convert.ToInt16(cbb_kontr.Value);
        }
        var query = regrar.DOGDOC.Where(d => d.DOGDOP.DOG1.KONTRAGENT == kontr &&
                                             d.NOMER == txt_DocNum.Text).Select(d => new
                                             {
                                                 CODE = d.CODE,
                                                 DOCTYPE = d.DOCTYPE1.NAME,
                                                 DOPDOG = d.DOPDOG,
                                                 NOMER = d.NOMER,
                                                 DATA = d.DATA,
                                                 PAGENUM = d.PAGENUM,
                                                 NOTE = d.NOTE,
                                                 ARCPLACE = d.ARCPLACE,
                                                 STATE = d.STATE
                                             });

        //query = query.OrderBy(d => d.DATA).Skip(iBeginRowIndex * iMaximumRows).Take(iMaximumRows);
        grid.DataSource = query.ToList();
        grid.DataBind();        
    }

    private void GetData(int iBeginRowIndex, int iMaximumRows)
    {
        //int kontr = Profile.UserInfo.ActiveKontragent;
        int kontr = Convert.ToInt32(cbb_kontr.Value);
        int docType = Convert.ToInt16(cbb_DocType.Value);
        string clause = string.Empty;
        DateTime d1 = dt1.Date;
        DateTime d2 = dt2.Date;

        var query =
            regrar.DOGDOC.Where(
                d => d.DOGDOP.DOG1.KONTRAGENT == kontr &&
                    ((docType > 0 && d.DOCTYPE == docType) || (docType == 0 && d.DOCTYPE == d.DOCTYPE)) &&
                    ((d1 != null && d.DATA >= d1 && d.DATA <= d2) || (d1 == null && d.DATA == d.DATA))).Select(d => new
                    {
                        CODE = d.CODE,
                        DOCTYPE = d.DOCTYPE1.NAME,
                        DOPDOG = d.DOPDOG,
                        NOMER = d.NOMER,
                        DATA = d.DATA,
                        PAGENUM = d.PAGENUM,
                        NOTE = d.NOTE,
                        ARCPLACE = d.ARCPLACE,
                        STATE = d.STATE
                    });
        //int PageCount = query.Count(); 
        //query = query.OrderBy(d => d.DATA).Skip(iBeginRowIndex * iMaximumRows).Take(iMaximumRows);
        grid.DataSource = query.ToList();
        grid.DataBind();
    }

    protected void grid_PageIndexChanged(object sender, EventArgs e)
    {
        GetData(grid.PageIndex, grid.SettingsPager.PageSize);
    }
    protected void grid_PageSizeChanged(object sender, EventArgs e)
    {
        GetData(grid.PageIndex, grid.SettingsPager.PageSize);
    }
}